package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.domain.Person;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.repository.PersonRepository;
import cn.wolfcode.luowowo.service.IPersonService;
import cn.wolfcode.luowowo.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Optional;

@Service
public class PersonServiceImpl implements IPersonService {

    @Autowired
    private PersonRepository repository;

    @Autowired
    private IUserInfoService userInfoService;



    public Person get(String id) {
        Optional<Person> op = repository.findById(id);
        if (op.isPresent()){
            return op.get();

        }
         return  null;
    }

    @Override
    public void save(Person person) {
        repository.save(person);
    }

    @Override
    public void update(Person person) {
        repository.save(person);
    }


    @Override
    public List<UserInfo> findByUserId(String id,String uid) {
        if (StringUtils.hasLength(id)) {
            Person person = repository.findByUserId(id);
            List<String> ids = person.getPersonId();
            if (!ids.contains(uid)){
                ids.add(uid);
            }
            if (!id.equals(uid)) {
                person.setTotalView(person.getTotalView() + 1);
            }
            this.update(person);
            List<UserInfo> list = userInfoService.findByIdIn(ids);
            return list;
        }

        return null;
    }

    @Override
    public List<UserInfo> findByUserIdAll(String owenId) {
        if (StringUtils.hasLength(owenId)) {
            Person person = repository.findByUserId(owenId);
            List<String> ids = person.getPersonId();
            List<UserInfo> list = userInfoService.findByIdAll(ids);
            return list;
        }

        return null;
    }

    @Override
    public Person findByUId(String owenerId) {
        return  repository.findByUserId(owenerId);
    }

//    @Override
//    public List<UserInfo> findByUserIdAll() {
//        return null;
//    }

//    @Override
//    public List<UserInfo> findByUserIdAll() {
//
//        Optional<Person> op = repository.findById("5ef9cfdb002c0000f7000f63");
//        if (op.isPresent()){
//            Person person = op.get();
//            List<String> ids = person.getPersonId();
//            List<UserInfo>  us =  userInfoService.findByInAll(ids);
//            return us;
//        }
//
//        return null;
//    }
}
